package com.yidu.qx.dao;

import com.yidu.qx.domain.QxCharacterEntity;
import com.yidu.qx.domain.QxModuleEntity;
import com.yidu.qx.utils.Pages;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @program: jurisdiction
 * @description: 模块表实现类
 * @author: 张超
 * @create: 2019-09-25 14:02
 */
public class ModuleDaoImpl implements ModuleDao{
    private HibernateTemplate hibernateTemplate;

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    @Override
    public List<QxModuleEntity> selectAll(QxModuleEntity qxModuleEntity) {
        DetachedCriteria criteria = DetachedCriteria.forClass(QxModuleEntity.class);

        criteria.add(Restrictions.eq("modState","on"));

        if(!StringUtils.isEmpty(qxModuleEntity.getModId())){

            criteria.add(Restrictions.eq("getTmModId",qxModuleEntity.getModId()));
        }else{

            criteria.add(Restrictions.eq("getTmModId","0"));
        }

        List<QxModuleEntity> list  = (List<QxModuleEntity>) hibernateTemplate.findByCriteria(criteria);

        return list;
    }

    @Override
    public void add(QxModuleEntity qxModuleEntity) {
        hibernateTemplate.save(qxModuleEntity);
    }

    @Override
    public void delete(QxModuleEntity qxModuleEntity) {
        hibernateTemplate.delete(qxModuleEntity);
    }

    @Override
    public void update(QxModuleEntity qxModuleEntity) {
        hibernateTemplate.update(qxModuleEntity);
    }

    @Override
    public List<QxModuleEntity> findAll(QxModuleEntity qxModuleEntity, Pages pages) {

        DetachedCriteria criteria = DetachedCriteria.forClass(QxModuleEntity.class);

        if(!StringUtils.isEmpty(qxModuleEntity.getModName())){
            criteria.add(Restrictions.like("modName","%"+qxModuleEntity.getModName()+"%"));
        }

        //过滤掉作废的
        //criteria.add(Restrictions.eq("modState","on"));

        //过滤掉最上层的
        criteria.add(Restrictions.ne("modId","0"));

        List<QxModuleEntity> list  = (List<QxModuleEntity>) hibernateTemplate.findByCriteria(criteria,pages.getStartRows(),pages.getLimit());
        return list;
    }


    @Override
    public QxModuleEntity findById(String id) {
        return hibernateTemplate.get(QxModuleEntity.class, id);
    }

    @Override
    public Long count(QxModuleEntity qxModuleEntity) {
        DetachedCriteria criteria = DetachedCriteria.forClass(QxModuleEntity.class);

        criteria.setProjection(Projections.rowCount());

        if(!StringUtils.isEmpty(qxModuleEntity.getModName())){
            criteria.add(Restrictions.like("modName","%"+qxModuleEntity.getModName()+"%"));
        }

        //过滤掉作废的
        //criteria.add(Restrictions.eq("modState","on"));

        //过滤掉最上层的
        criteria.add(Restrictions.ne("modId","0"));

        Long count = (Long)hibernateTemplate.findByCriteria(criteria).get(0);

        return count;
    }
}